﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TowerDefence.Entities.States;
using TowerDefence.Tiles;
using Microsoft.Xna.Framework;

namespace TowerDefence.Entities.Actions
{
    public class PathFinder : Action
    {
        ITileLocator tileLocator;
        eAIPath aiPath;

        public PathFinder(
            ITileLocator _tileLocator, 
            eAIPath _aiPath, 
            Entity _userEntity, 
            params Entity[] _targetEntities) 
            : base(_userEntity, _targetEntities)
        {
            this.tileLocator = _tileLocator;
            this.aiPath = _aiPath;
        }

        public override void DoAction(TimeSpan _time)
        {
            // get tile direction from tile locator
            var tile = this.tileLocator.GetTile(this.userEntity.Form.Position) as PathTile;
            if(tile == null) return;

            var movementVector = this.userEntity.Form.States.GetState<MovementVector>();
            if (movementVector == null) return;
            var speed = this.userEntity.Form.States.GetState<Speed>();
            if (speed == null) return;
            switch (tile.GetDirectionForPath(this.aiPath))
            {
                // Negative Y is up (origin is top-left)
                case eAIDir.UP:
                    movementVector.Vector = new Vector2(0, -speed.Amount);
                    break;
                case eAIDir.DOWN:
                    movementVector.Vector = new Vector2(0, speed.Amount);
                    break;
                case eAIDir.LEFT:
                    movementVector.Vector = new Vector2(-speed.Amount, 0);
                    break;
                case eAIDir.RIGHT:
                    movementVector.Vector = new Vector2(speed.Amount, 0);
                    break;
            }
        }
    }
}
